/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fightplan.sandbox;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JSeparator;

import sun.swing.SwingUtilities2;

/**
 *
 * @author Bruno
 */
public class J6 extends JLabel {

    protected boolean intend = true;

    private JSeparator separator = new JSeparator();

    private int textSpacing = 2;

    public J6() {
        super();
    }

    public J6(Icon image) {
        super(image);
    }

    public J6(Icon image, int horizontalAlignment) {
        super(image, horizontalAlignment);
    }

    public J6(String text) {
        super(text);
    }

    public J6(String text, int horizontalAlignment) {
        super(text, horizontalAlignment);
    }

    public J6(String text, Icon icon, int horizontalAlignment) {
        super(text, icon, horizontalAlignment);
    }

    public boolean isIntend() {
        return intend;
    }

    public void setIntend(boolean intend) {
        this.intend = intend;
    }

    public int getTextSpacing() {
        return textSpacing;
    }

    public void setTextSpacing(int textSpacing) {
        this.textSpacing = textSpacing;
    }

    @Override
    public void paint(Graphics g) {

        FontMetrics fm = SwingUtilities2.getFontMetrics(this, g);
        int descent = fm.getDescent();

        int justification = getHorizontalAlignment();
        if (getComponentOrientation().isLeftToRight()) {
            if (getHorizontalAlignment() == LEADING) {
                justification = LEFT;
            } else if (getHorizontalAlignment() == TRAILING) {
                justification = RIGHT;
            }
        } else {
            if (getHorizontalAlignment() == LEADING) {
                justification = RIGHT;
            } else if (getHorizontalAlignment() == TRAILING) {
                justification = LEFT;
            }
        }

        int textLocX = 0;
        int prefWidth = super.getPreferredSize().width;

        switch (justification) {
            case LEFT:
                textLocX = isIntend() ? 10 + textSpacing : 0;
                break;
            case RIGHT:
                textLocX = getWidth() - prefWidth;
                if (isIntend()) textLocX = textLocX - 10 - textSpacing;
                break;
            case CENTER:
                textLocX = (getWidth() - prefWidth) / 2 - textSpacing;
                break;
        }

        int textEnd = textLocX + prefWidth;

        ////

        Rectangle r = getBounds();

        if (textLocX > 0)
        {
            Graphics g2 = g.create(0, getBaseline(r.width, r.height) - descent / 2, textLocX - textSpacing, 2);
            separator.setSize(textLocX - textSpacing, 2);
            separator.paint(g2);
        }

        if (textEnd < getWidth())
        {
            Graphics g2 = g.create(textEnd + textSpacing, getBaseline(r.width, r.height) - descent / 2, getWidth() - textEnd - textSpacing, 2);
            separator.setSize(getWidth() - textEnd - textSpacing, 2);
            separator.paint(g2);
        }

        //

        Graphics g2 = g.create(textLocX, 0, prefWidth, getHeight());
        setBounds(0, 0, prefWidth, getHeight());
        super.paint(g2);
        setBounds(r);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(super.getPreferredSize().width + textSpacing + 10 + (isIntend() ? textSpacing + 10 : 0), super.getPreferredSize().height);
    }

}
